﻿using System;
using System.Collections.Generic;
using System.Xml;
using ZofX.NpoiXHelper.Lib;

namespace ZofX.NpoiXHelper.Model
{
    /// <summary>
    /// 配置读取类
    /// </summary>
    public static class Config
    {
        static AppConfigHelper xml;

        static Config()
        {
            xml = new AppConfigHelper(AppDomain.CurrentDomain.BaseDirectory + "npoi.config");
        }

        /// <summary>
        /// 获取导出设置
        /// </summary>
        /// <param name="Code">导出设置编码</param>
        /// <returns>设置信息</returns>
        public static Table GetXlsExporterConfig(string Code)
        {
            Table t = new Table();
            t.Code = Code;
            t.Name = xml.GetNodeAttr("/Config/XlsExporterConfig", "Table", "Code", Code, "Name");
            t.Columns = new List<Column>();

            XmlNodeList list = xml.GetChildNodes("/Config/XlsExporterConfig/Table[@Code='" + Code + "']/Column");
            foreach (XmlNode node in list)
            {
                Column c = new Column();
                c.Code = node.Attributes["Code"].Value;
                c.Formart = node.Attributes["Formart"].Value;
                c.Name = node.Attributes["Name"].Value;
                c.Property = node.Attributes["Property"].Value;
                c.Type = node.Attributes["Type"].Value;
                int order;
                int.TryParse(node.Attributes["Order"].Value, out order);
                c.Order = order;
                t.Columns.Add(c);
            }
            return t;
        }

        /// <summary>
        /// 获取导入设置
        /// </summary>
        /// <param name="Code">导入设置编码</param>
        /// <returns>设置信息</returns>
        public static Table GetXlsImporterConfig(string Code)
        {
            Table t = new Table();
            t.Code = Code;
            t.Name = xml.GetNodeAttr("/Config/XlsImporterConfig", "Table", "Code", Code, "Name");
            t.Columns = new List<Column>();

            XmlNodeList list = xml.GetChildNodes("/Config/XlsImporterConfig/Table[@Code='" + Code + "']/Column");
            foreach (XmlNode node in list)
            {
                Column c = new Column();
                c.Code = node.Attributes["Code"].Value;
                c.Formart = node.Attributes["Formart"].Value;
                c.Name = node.Attributes["Name"].Value;
                c.Property = node.Attributes["Property"].Value;
                c.Type = node.Attributes["Type"].Value;
                int order;
                int.TryParse(node.Attributes["Order"].Value, out order);
                c.Order = order;
                t.Columns.Add(c);
            }
            return t;
        }
    }
}
